<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <title>工具集</title>
    <meta charset="utf-8"/>
    <meta name="renderer" content="webkit">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="shortcut icon" href="../static/images/favicon/favicon.ico"
          th:href="@{/images/favicon/favicon.ico}">
    <link rel="stylesheet" type="text/css" href="/layuiDemo/static/plugins/layui/css/layui.css"
          th:href="@{/plugins/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" type="text/css" href="../static/css/global.css"
          th:href="@{/css/global.css}" media="all">
    <link rel="stylesheet" type="text/css" href="/layuiDemo/static/plugins/font-awesome-4.7.0/css/font-awesome.css"
          th:href="@{/plugins/font-awesome-4.7.0/css/font-awesome.css}">
</head>

<body>
<div style="margin: 15px;">

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
        <legend>固定块</legend>
    </fieldset>

    <p>囖，就页面右下角的那个。</p>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>倒计时</legend>
    </fieldset>
    请选择要计算的日期：
    <div class="layui-inline">
        <input type="text" class="layui-input" id="test1" value="2099-01-01 00:00:00">
    </div>
    <blockquote class="layui-elem-quote" style="margin-top: 10px;">
        <div id="test2"></div>
    </blockquote>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend>某个时间在当前时间的多久前</legend>
    </fieldset>

    请选择要计算的日期：
    <div class="layui-inline">
        <input type="text" class="layui-input" id="test3">
    </div>

    <span class="layui-word-aux" id="test4"></span>
</div>
<script type="text/javascript" src="/layuiDemo/static/plugins/layui/layui.js"
        th:src="@{/plugins/layui/layui.js}"></script>
<script>

    layui.use(['util', 'laydate', 'layer'], function () {
        var util = layui.util
            , laydate = layui.laydate
            , layer = layui.layer;
        //固定块
        util.fixbar({
            bar1: true
            , bar2: true
            , css: {right: 50, bottom: 100}
            , bgcolor: '#393D49'
            , click: function (type) {
                if (type === 'bar1') {
                    layer.msg('icon是可以随便换的')
                } else if (type === 'bar2') {
                    layer.msg('两个bar都可以设定是否开启')
                }
            }
        });

        //倒计时
        var thisTimer, setCountdown = function (y, M, d, H, m, s) {
            var endTime = new Date(y, M || 0, d || 1, H || 0, m || 0, s || 0) //结束日期
                , serverTime = new Date(); //假设为当前服务器时间，这里采用的是本地时间，实际使用一般是取服务端的

            clearTimeout(thisTimer);
            util.countdown(endTime, serverTime, function (date, serverTime, timer) {
                var str = date[0] + '天' + date[1] + '时' + date[2] + '分' + date[3] + '秒';
                lay('#test2').html(str);
                thisTimer = timer;
            });
        };
        setCountdown(2099, 1, 1);

        laydate.render({
            elem: '#test1'
            , type: 'datetime'
            , done: function (value, date) {
                setCountdown(date.year, date.month - 1, date.date, date.hours, date.minutes, date.seconds);
            }
        });


        //某个时间在当前时间的多久前
        var setTimeAgo = function (y, M, d, H, m, s) {
            var str = util.timeAgo(new Date(y, M || 0, d || 1, H || 0, m || 0, s || 0));
            lay('#test4').html(str);
        };

        laydate.render({
            elem: '#test3'
            , type: 'datetime'
            , done: function (value, date) {
                setTimeAgo(date.year, date.month - 1, date.date, date.hours, date.minutes, date.seconds);
            }
        });

    });
</script>
</body>

</html>